package com.sinosoft.system.bank.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sinosoft.system.bank.dao.BankTradeDao;
import com.sinosoft.system.bank.entity.BankTrade;

   /**
    * @ClassName BankTradeDaoJdbc实现类
    * @date 2017-2-16 
    * @author shangxp
    */  
@Repository
public class BankTradeDaoJdbc implements BankTradeDao {
	private static Logger log = Logger.getLogger(BankTradeDaoJdbc.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int insertBankTrade(BankTrade bankTrade) {
		String sqlStr = "insert into bank_trade ( "
							+" id ,"	
							+" sys_user_id ,"	
							+" tradename ,"	
							+" tradeint ,"	
							+" tradermb ,"	
							+" trade ,"	
							+" tradetime ,"	
							+" tradeinfo ,"	
							+" remark ,"	
							+"is_del) values( "
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" 0 )";
		log.debug(sqlStr+"\n"+bankTrade.toString());
		try{
			Object[] args = {
					bankTrade.getId(),
					bankTrade.getSysUserId(),
					bankTrade.getTradename(),
					bankTrade.getTradeint(),
					bankTrade.getTradermb(),
					bankTrade.getTrade(),
					bankTrade.getTradetime(),
					bankTrade.getTradeinfo(),
					bankTrade.getRemark()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据添加异常 SQL："+sqlStr+"\n"+bankTrade.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int updateBankTrade(BankTrade bankTrade) {
		String sqlStr = "update bank_trade set "
									+" id = ? , "
									+" sys_user_id = ? , "
									+" tradename = ? , "
									+" tradeint = ? , "
									+" tradermb = ? , "
									+" trade = ? , "
									+" tradetime = ? , "
									+" tradeinfo = ? , "
									+" remark = ?  "
									+" where id = ?";
		log.debug(sqlStr+"\n"+bankTrade.toString());
		try{
			Object[] args = {
					bankTrade.getId(),
					bankTrade.getSysUserId(),
					bankTrade.getTradename(),
					bankTrade.getTradeint(),
					bankTrade.getTradermb(),
					bankTrade.getTrade(),
					bankTrade.getTradetime(),
					bankTrade.getTradeinfo(),
					bankTrade.getRemark(),
					bankTrade.getId()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据更新异常 SQL："+sqlStr+"\n"+bankTrade.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int deleteBankTrade(int id) {
		String sqlStr = "update bank_trade set is_del = 1  where id = ?";
		try{
			Object[] args = {
					id
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return 0;
	}

	@Override
	public BankTrade selectBankTrade(int id) {
		String sqlStr = "select * from bank_trade where is_del =0 and id = ?";
		log.debug(sqlStr+"\nID="+id);
		try{
			Object[] args = {
				id
			};
			return jdbcTemplate.queryForObject(sqlStr, args, new BankTradeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return null;
	}

	@Override
	public BankTrade selectBankTrade(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from bank_trade where is_del =0 " );
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+"=? ");
				args[i] = queryValues.get(i);
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args, new BankTradeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountBankTrade() {
		String sqlStr = "select count(1) from bank_trade where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.queryForObject(sqlStr, Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<BankTrade> selectLimitBankTrade(int start, int size) {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" sys_user_id ,";
					sqlStr+=" tradename ,";
					sqlStr+=" tradeint ,";
					sqlStr+=" tradermb ,";
					sqlStr+=" trade ,";
					sqlStr+=" tradetime ,";
					sqlStr+=" tradeinfo ,";
					sqlStr+=" remark ";
			sqlStr+=" from bank_trade where is_del =0  order by id desc limit ?,?";
		log.debug(sqlStr);
		try{
		Object[] args = {
				start,
				size
		};
			return jdbcTemplate.query(sqlStr, args, new BankTradeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<BankTrade> selectAllBankTrade() {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" sys_user_id ,";
					sqlStr+=" tradename ,";
					sqlStr+=" tradeint ,";
					sqlStr+=" tradermb ,";
					sqlStr+=" trade ,";
					sqlStr+=" tradetime ,";
					sqlStr+=" tradeinfo ,";
					sqlStr+=" remark ";
		 sqlStr+=" from bank_trade where is_del =0 order by id desc ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.query(sqlStr, new BankTradeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountBankTrade(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select count(1) from bank_trade where is_del = 0 ");
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
				args[i] = queryValues.get(i) ;
			}
		}		
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args,Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<BankTrade> selectLimitBankTrade(List<String> queryKeys,List<Object> queryValues,int start,int size) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" sys_user_id ,");
					sqlStr.append(" tradename ,");
					sqlStr.append(" tradeint ,");
					sqlStr.append(" tradermb ,");
					sqlStr.append(" trade ,");
					sqlStr.append(" tradetime ,");
					sqlStr.append(" tradeinfo ,");
					sqlStr.append(" remark ");
			sqlStr.append(" from bank_trade where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()+2];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
					args[i] =  queryValues.get(i) ;
				}
			}
		if(args==null){
			args = new Object[]{start,size};
		}else{
			args[queryKeys.size()] = start;
			args[queryKeys.size()+1] = size;
		}
		sqlStr.append(" order by id desc ");
		sqlStr.append(" limit ?,?");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new BankTradeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<BankTrade> selectAllBankTrade(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" sys_user_id ,");
					sqlStr.append(" tradename ,");
					sqlStr.append(" tradeint ,");
					sqlStr.append(" tradermb ,");
					sqlStr.append(" trade ,");
					sqlStr.append(" tradetime ,");
					sqlStr.append(" tradeinfo ,");
					sqlStr.append(" remark ");
			sqlStr.append("from bank_trade where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
					args[i] = queryValues.get(i);
				}
			}
		sqlStr.append(" order by id desc ");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new BankTradeMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	protected class BankTradeMapper implements RowMapper<BankTrade> {
		@Override
		public BankTrade mapRow(ResultSet arg0, int arg1) throws SQLException {
			BankTrade bankTrade = new BankTrade();
			if(arg0!=null){
					bankTrade.setId(arg0.getInt("id"));
					bankTrade.setSysUserId(arg0.getInt("sys_user_id"));
					bankTrade.setTradename(arg0.getString("tradename"));
					bankTrade.setTradeint(arg0.getString("tradeint"));
					bankTrade.setTradermb(arg0.getString("tradermb"));
					bankTrade.setTrade(arg0.getString("trade"));
					bankTrade.setTradetime(arg0.getString("tradetime"));
					bankTrade.setTradeinfo(arg0.getString("tradeinfo"));
					bankTrade.setRemark(arg0.getString("remark"));
				}
			return bankTrade;
		}  
	}
}

